package com.xbongbong.paas.service.dynamic.crm.strategy.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.pojo.dto.SaasFormSaveDTO;
import com.xbongbong.paas.service.dynamic.crm.strategy.AbstractDynamicStrategy;
import com.xbongbong.parent.interfaces.DynamicStrategy;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicDeleteDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicRefInfoDTO;
import com.xbongbong.pro.dynamic.pojo.dto.DynamicSaveDTO;
import com.xbongbong.saas.enums.ProjectLogEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.InvoiceEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *发票
 *
 *@author weijianxun
 */
@Service("invoiceDynamicStrategyImpl")
public class InvoiceDynamicStrategyImpl extends AbstractDynamicStrategy implements DynamicStrategy {

    private final Logger LOG = LoggerFactory.getLogger(InvoiceDynamicStrategyImpl.class);

    private static final Map<String, Integer> refTextToLogTypeMap = new HashMap<>();

    static {
        refTextToLogTypeMap.put(InvoiceEnum.CUSTOMER_NAME.getAttr(), ProjectLogEnum.CUSTOMER_REF.getSubType());
        refTextToLogTypeMap.put(InvoiceEnum.CONTRACT_ID.getAttr(), ProjectLogEnum.CONTRACT_REF.getSubType());
    }

    public static Map<String, Integer> getRefTextToLogTypeMap(){
        return Collections.unmodifiableMap(refTextToLogTypeMap);
    }


    @Override
    public List<Integer> type() {
        return Arrays.asList(XbbRefTypeEnum.INVOICE.getCode());
    }

    @Override
    @Async("dynamicThreadPool")
    public void save(DynamicSaveDTO dynamicSaveDTO) {
        try {
            SaasFormSaveDTO saasFormSaveDTO = dynamicSaveDTO.getSaasFormSaveDTO();
            PaasFormDataEntity paasFormDataEntity = saasFormSaveDTO.getNewPaasFormDataEntity();
            Long dataId = paasFormDataEntity.getId();
            saveRefUpDynamic(dynamicSaveDTO, refTextToLogTypeMap, null, DynamicRefInfoDTO.initDynamicRefInfoDTO(dataId,
                    saasFormSaveDTO.getBusinessType(), paasFormDataEntity.getSerialNo()), ProjectLogEnum.INVOICE_CREATE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.InvoiceDynamicStrategyImpl.save", e);
        }
    }

    @Override
    @Async("dynamicThreadPool")
    public void batchDelete(DynamicDeleteDTO dynamicDeleteDTO) {
        try {
            super.batchRefSaveUpDynamicByDeleteBatch(dynamicDeleteDTO, refTextToLogTypeMap, null, InvoiceEnum.SHEET_NO.getAttrName(), ProjectLogEnum.INVOICE_DELETE.getSubType());
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.dynamic.crm.strategy.impl.InvoiceDynamicStrategyImpl.batchDelete", e);
        }
    }
}
